package com.zp.datax.controller;

import com.zp.datax.core.model.DataxJobInfo;
import com.zp.datax.core.result.ReturnT;
import com.zp.datax.service.DataxJobInfoService;
import com.zp.datax.service.DataxJobLogService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 日志管理
 */
@Controller
@RequestMapping("/joblog")
public class DataxJobLogController {

    @Resource
    private DataxJobLogService jobLogService;
    @Resource
    private DataxJobInfoService jobInfoService;


    @RequestMapping("")
    public String index(Model model, @RequestParam(required = false) Integer taskId) {

        List<DataxJobInfo> list = jobInfoService.searchAll();
        model.addAttribute("filenameList", list);

        model.addAttribute("taskId", taskId);

        return "joblog/joblog.index";
    }

    @RequestMapping("/pageList")
    @ResponseBody
    public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int length,
                                        int taskId, int status, String filterTime) {

        // parse param
        String startTime = null;
        String endTime = null;
        if (filterTime != null && filterTime.trim().length() > 0) {
            String[] temp = filterTime.split(" - ");
            if (temp != null && temp.length == 2) {
                startTime = temp[0].replace(" ", "T");
                endTime = temp[1].replace(" ", "T");
            }
        }

        return jobLogService.pageList(start, length, taskId, status, startTime, endTime);
    }

    @RequestMapping("/logDetailPage")
    public String logDetailPage(int sysId, Model model) {
        ReturnT<String> returnT = jobLogService.logDetailPage(sysId);

        model.addAttribute("logDetail", returnT.getData());
        return "joblog/joblog.detail";
    }

}
